﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>节点时限设置</title>
    <script src="../Scripts/easyUI145/jquery.min.js" type="text/javascript"></script>
    <script src="../Scripts/config.js" type="text/javascript"></script>
    <script src="../Comm/Gener.js" type="text/javascript"></script>
    <script src="../Scripts/QueryString.js" type="text/javascript"></script>
    <link href="../Scripts/layui/layui/css/layui.css" rel="stylesheet" />
    <link href="../Scripts/layui/style/admin.css" rel="stylesheet" />
    <script src="../Scripts/layui/layui/layui.js" type="text/javascript"></script>
    <style type="text/css">
        .table > tbody > tr > td, .table > tbody > tr > th, .table > tfoot > tr > td, .table > tfoot > tr > th, .table > thead > tr > td, .table > thead > tr > th, .table > colgroup + thead > tr:first-child > th {
            border: 1px solid rgb(221, 221, 221);
        }

        .ccdate {
            padding-left: 25px;
        }
        /*滚动条*/
        ::-webkit-scrollbar-thumb {
            background-color: #b5b5b5;
            height: 50px;
            -webkit-border-radius: 4px;
        }

            ::-webkit-scrollbar-thumb:hover {
                background-color: #585858;
                height: 50px;
                -webkit-border-radius: 4px
            }

        ::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }

        ::-webkit-scrollbar-track-piece {
            background-color: #fff;
            -webkit-border-radius: 4px
        }
    </style>
    <script type="text/javascript">
        var currtab = GetQueryString("CurrTab");
        var fk_node = GetQueryString("FK_Node");
        var fk_flow = GetQueryString("FK_Flow");
        var workid = GetQueryString("WorkID");
        var fid = GetQueryString("FID");
        var webUser = new WebUser();
        var isReadOnly = GetQueryString("IsReadOnly");
        isReadOnly = isReadOnly == null || isReadOnly == undefined || isReadOnly == "" ? "0" : isReadOnly;
        var startNodeID = parseInt(fk_flow) + "01";

        $(function () {
            InitPage();
        });

        //初始化数据
        function InitPage() {

            var handler = new HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            handler.AddUrlData();
            var data = handler.DoMethodReturnString("CH_Init");
            if (data.indexOf("err@") == 0) {
                alert(data);
                return;
            }

            data = JSON.parse(data);
            var chNodes = data["WF_CHNode"];
            var gwls = data["WF_GenerWorkerList"];
            var node = data.WF_CurrNode[0];
            //流程时限属性
            var gwf = new Entity("BP.WF.GenerWorkFlow");
            gwf.CopyJSON(data.WF_GenerWorkFlow[0]);
            //节点时限
            var tableData = [];


            //判断是否可以修改时限
            var chRole = node.CHRole;
            if ((chRole == 1 || chRole == 3) && isReadOnly != "1") {
                $("#bottomBar").show();
                $('#SaveBtn').on('click', function () {
                    Save();
                });
            }
              

            var _Html = "";
            var idx = 0;
            var plantStartDt; // 计划开始时间
            var sdtOfNode; //计划完成时间
            var GS;
            var Pro;
            var limintT;
            for (var i = 0; i < chNodes.length; i++) {
                idx = i + 1;
                var chNode = chNodes[i];
                var item = {};
                item.idx = idx;
                //已经运行过的节点或者运行到的节点
                if (chNode.AtPara != "" && chNode.AtPara != undefined) {
                    item.NodeName = chNode.NodeName;
                    item.NodeID = chNode.FK_Node;
                    item.EmpName = chNode.FK_EmpT;
                    item.StartDT = chNode.StartDT;
                    item.EndDT = chNode.EndDT
                    item.GT = chNode.GT;
                    item.Scale = chNode.Scale;
                    item.TotalScale = chNode.TotalScale;
                    var isPass = GetPara(chNode.AtPara, "IsPass");
                    if (isPass != 1) {
                        item.IsEdit = 1;
                    } else {
                        item.IsEdit = 0;
                    }


                    if (chNode.FK_Node == parseInt(fk_flow) + "01") { // 开始节点
                        item.HS = "无";
                        item.WFState = "正常";
                    } else {
                        //耗时
                        var rdt = GetPara(chNode.AtPara, "RDT");
                        rdt = new Date(Date.parse(rdt.replace(/-/g, "/")));
                        var cdt = GetPara(chNode.AtPara, "CDT");
                        cdt = new Date(Date.parse(cdt.replace(/-/g, "/")));

                        item.HS = GetSpanTime(rdt, cdt);
                        //状态
                        if (cdt <= chNode.EndDT)
                            item.WFState = "正常";
                        else
                            item.WFState = "逾期";

                    }
                } else { //未运行到的节点
                    if (chNode.FK_Node == parseInt(fk_flow) + "01") { // 开始节点
                        item.NodeName = chNode.NodeName;
                        item.NodeID = chNode.FK_Node;
                        item.EmpName = webUser.Name;
                        item.StartDT = new Date().format("yyyy-MM-dd hh:mm");
                        item.EndDT = "无";
                        item.GT = chNode.GT;
                        item.Scale = chNode.Scale;
                        item.TotalScale = chNode.TotalScale;
                        item.ADT = new Date().format("yyyy-MM-dd hh:mm");
                        item.ComplateDT = "无";
                        item.HS = "无";
                        item.WFState = "正常";
                        item.IsEdit = 1;
                    } else {
                        item.NodeName = chNode.NodeName;
                        item.NodeID = chNode.FK_Node;
                        item.EmpName = "";
                        if (isReadOnly == "1") {
                            item.StartDT = chNode.StartDT;
                            item.EndDT = chNode.EndDT;
                            item.GT = chNode.GT;
                            item.Scale = chNode.Scale;
                            item.TotalScale = chNode.TotalScale;
                            item.IsEdit = 0;
                        } else {
                            if (chRole == 1 || chRole == 3 || isReadOnly == "0") {
                                item.StartDT = chNode.StartDT;
                                item.EndDT = chNode.EndDT;
                                item.GT = chNode.GT;
                                item.Scale = chNode.Scale;
                                item.TotalScale = chNode.TotalScale;
                                item.IsEdit = 1;
                            } else {
                                item.StartDT = chNode.StartDT;
                                item.EndDT = chNode.EndDT;
                                item.GT = chNode.GT;
                                item.Scale = chNode.Scale;
                                item.TotalScale = chNode.TotalScale;
                                item.IsEdit = 0;
                            }
                        }
                        item.ADT = "-";
                        item.ComplateDT = "-";
                        item.HS = "-";
                        item.WFState = "-";
                    }
                }
                tableData.push(item);

            }

            layui.use(['table', 'util'], function () {
                var table = layui.table
                    , laydate = layui.laydate;

                //显示节点时限的数据信息
                var vtable = table.render({
                    elem: '#node',
                    id: 'node',
                    data: tableData,
                    title: '数据表',
                    limit: Number.MAX_VALUE,
                    cellMinWidth: 120,
                    size: 'lg',
                    page: false,
                    cols: [[
                        { field: 'idx', title: '序号', minWidth: 5 }
                        , { field: 'NodeName', title: '节点', minWidth: 140, sort: false }
                        , { field: 'EmpName', title: '处理人', minWidth: 80, sort: false }
                        , {
                            field: 'StartDT', type: 'date', title: '计划开始时间', minWidth: 180,
                            templet: function (row) {
                                if (row.IsEdit == 1 && row.NodeID != startNodeID)
                                    return '<div class="layui-input-inline"><i class="input-icon layui-icon layui-icon-date"></i><input type="text" id="' + this.field + '_' + row["NodeID"] + '" name="' + this.field + '_' + row["NodeID"] + '" class="layui-input ccdate" value="' + row[this.field] + '"></div>';
                                return row[this.field];
                            }
                        }
                        , {
                            field: 'EndDT', type: 'date', title: '计划完成时间', minWidth: 180,
                            templet: function (row) {
                                if (row.IsEdit == 1 && row.NodeID != startNodeID)
                                    return '<div class="layui-input-inline"><i class="input-icon layui-icon layui-icon-date"></i><input type="text" id="' + this.field + '_' + row["NodeID"] + '" name="' + this.field + '_' + row["NodeID"] + '" class="layui-input ccdate" value="' + row[this.field] + '"></div>';
                                return row[this.field];
                            }
                        }
                        , {
                            field: 'GT', type: 'number', title: '工天(天)', minWidth: 80,
                            templet: function (row) {
                                if (row.IsEdit == 1 && row.NodeID != startNodeID)
                                    return '<input type="number" name="' + this.field + '_' + row["NodeID"] + '" step="1" class="layui-input" value="' + row[this.field] + '"  onchange="ChangeStyle(this)">';
                                return row[this.field];
                            }
                        }
                        , {
                            field: 'Scale', type: 'number', title: '阶段占比(%)', minWidth: 80,
                            templet: function (row) {
                                if (row.IsEdit == 1)
                                    return '<input type="number" name="' + this.field + '_' + row["NodeID"] + '" step="1" class="layui-input" value="' + row[this.field] + '"  onchange="ChangeStyle(this)"';
                                return row[this.field];
                            }
                        }
                        , {
                            field: 'TotalScale', type: 'number', title: '总体进度(%)', minWidth: 80,
                            templet: function (row) {
                                if (row.IsEdit == 1)
                                    return '<input type="number" name="' + this.field + '_' + row["NodeID"] + '" step="1" class="layui-input  layui-table-edit" value="' + row[this.field] + '"  onchange="ChangeStyle(this)">';
                                return row[this.field];
                            }
                        }
                        , { field: 'ADT', title: '任务到达时间', minWidth: 160, sort: false }
                        , { field: 'ComplateDT', title: '实际完成时间', minWidth: 160, sort: false }
                        , { field: 'HS', title: '耗时', minWidth: 60 }
                        , { field: 'WFState', title: '状态', minWidth: 60 }
                        , { field: 'IsEdit', title: '是否编辑', hide: true }
                    ]]
                });

               
                
                $.each($(".ccdate"), function (i, item) {
                    laydate.render({
                        elem: '#' + item.id,
                        type: 'datetime',
                        format: 'yyyy-MM-dd HH:mm',
                        done: function (value, date, endDate) {
                            var val = $(this.elem).val();
                            if (val != value) {
                                $(this.elem).css("background-color", "#5fb87852");
                            }
                               
                        }
                    });
                });
                var flow = data.WF_Flow[0];
                if (flow.SDTOfFlowRole != 0) {
                    $("#flow").hide();
                    return;
                }
                tableData = [];
                var item = {};
                item.StartDT = gwf.RDT;
                if (gwf.SDTOfFlow == null || gwf.SDTOfFlow == "" || gwf.SDTOfFlow == undefined) {
                    item.SDTOfFlow="无";
                    item.SYSJ="0秒";
                    item.State="无";
                } else {
                    item.SDTOfFlow =gwf.SDTOfFlow ;
                    var sdtofFlow = new Date(Date.parse(gwf.SDTOfFlow.replace(/-/g, "/")));
                    if (gwf.SDTOfFlow <= new Date().format("yyyy-MM-dd HH:mm")) {
                        item.SYSJ =GetSpanTime(sdtofFlow, new Date());
                        item.State ="逾期";

                    } else {
                        var spanTime = data.SpanTime[0].SpanTime;
                         item.SYSJ =spanTime;
                         item.State ="正常";
                    }
                }
                if (chRole == 3 && isReadOnly == "0")
                    item.IsHidden = 0;
                else
                    item.IsHidden = 1;
                tableData.push(item);
                //显示流程时限的数据信息
                var vtable = table.render({
                    elem: '#flow',
                    id: 'flow',
                    data: tableData,
                    title: '数据表',
                    limit: Number.MAX_VALUE,
                    cellMinWidth: 120,
                    size: 'lg',
                    page: false,
                    cols: [[
                        {field: 'StartDT', type: 'date', title: '开始日期', minWidth: 180}
                        ,{field: 'SDTOfFlow', type: 'date', title: '应完成日期', minWidth: 180,}
                        , {field: 'SYSJ', type: 'number', title: '剩余时间', minWidth: 80,}
                        , {field: 'State', type: 'number', title: '状态', minWidth: 80,}
                        
                        , {
                            field: 'IsChangeDT', title: '调整时间', minWidth: 160, templet: function (row) {
                                if (row.IsHidden == 0)
                                    return '<div class="layui-input-inline"><i class="input-icon layui-icon layui-icon-date"></i><input type="text" id="GWF" name="GWF" class="layui-input ccdate"   value="' + gwf.SDTOfFlow + '"></div>';
                                return "-";
                            }}
                        
                    ]]
                });

                laydate.render({
                    elem: '#GWF',
                    type: 'datetime',
                    format: 'yyyy-MM-dd HH:mm',
                    done: function (value, date, endDate) {
                        var val = $(this.elem).val();
                        if (val != value) {
                            $(this.elem).css("background-color", "#5fb87852");
                        }

                    }
                });
            });
        }

        /**
         * 改变背景颜色
         * @param obj 元素DOM
         */
        function ChangeStyle(obj) {
            $(obj).css("background-color", "#5fb87852");
        }

        //比对时间
        function CompareDate(nodeId, type) {
            var startDt = $("#PlantStartDt_" + nodeId).val();
            var endDt = $("#CH_" + nodeId).val();
            if (startDt > endDt) {
                if (type == 0) {
                    alert("计划开始时间不能大于计划完成时间");
                    $("#PlantStartDt_" + nodeId).focus();
                }
                else {
                    alert("计划完成时间不能小于计划开始时间");
                    $("#CH_" + nodeId).focus();
                }
            } else {
                changeStyle(nodeId, type);
            }
            return;
        }

        //保存设置的时限
        function Save() {
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_WorkOpt");
            handler.AddFormData();
            handler.AddUrlData();
            var data = handler.DoMethodReturnString("CH_Save");
            if (data.indexOf("err@") == 0) {
                alert(data);
                return;
            }
            alert("保存成功");
            Reload();

        }

        //根据NodeID获取单实体
        function GetEntityByNode(gwls, nodeID) {
            for (var idx = 0; idx < gwls.length; idx++) {
                if (gwls[idx].FK_Node == nodeID)
                    return gwls[idx];
            }
            return null;
        }

        //剩余多少天
        function GetOverSpanTime(date1, date2) {
            //获取当前日期
            var currDate = new Date();
            if (currDate > date2)
                return "0天";
            return GetSpanTime(currDate, date2);

        }
        function GetSpanTime(date1, date2) {
            ///<summary>计算date2-date1的时间差，返回使用“x天x小时x分x秒”形式的字符串表示</summary>
            var date3 = date2.getTime() - date1.getTime();  //时间差秒
            var str = '';
            //计算出相差天数
            var days = Math.floor(date3 / (24 * 3600 * 1000));
            if (days > 0) {
                str += days + '天';
            }

            //计算出小时数
            var leave1 = date3 % (24 * 3600 * 1000);   //计算天数后剩余的毫秒数
            var hours = Math.floor(leave1 / (3600 * 1000));
            if (hours > 0) {
                str += hours + '小时';
            }

            //计算相差分钟数
            var leave2 = leave1 % (3600 * 1000);         //计算小时数后剩余的毫秒数
            var minutes = Math.floor(leave2 / (60 * 1000));
            if (minutes > 0) {
                str += minutes + '分';
            }

            if (str.length == 0) {
                var leave3 = leave2 % (60 * 1000);
                var seconds = Math.floor(leave3 / 1000);

                str += seconds + '秒';

                if (seconds == NaN)
                    return date1 + "," + date2;
                return str;
            }

            return str;
        }

        //时间格式
        Date.prototype.format = function (format) {
            var date = {
                "M+": this.getMonth() + 1,
                "d+": this.getDate(),
                "h+": this.getHours(),
                "m+": this.getMinutes(),
                "s+": this.getSeconds(),
                "q+": Math.floor((this.getMonth() + 3) / 3),
                "S+": this.getMilliseconds()
            };
            if (/(y+)/i.test(format)) {
                format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            for (var k in date) {
                if (new RegExp("(" + k + ")").test(format)) {
                    format = format.replace(RegExp.$1, RegExp.$1.length == 1
                        ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
                }
            }
            return format;
        }

    </script>

</head>
<body>
    <div style="padding:12px">
        <form id="cc">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-card-header">节点时限</div>
                    <table class="layui-table" lay-filter="node" id="node"></table>
                    <div class="layui-card-header">流程时限</div>
                    <table class="layui-table" lay-filter="flow" id="flow"></table>
                </div>
                <div id="bottomBar"  style="display:none;margin:10px 0px 10px 10px">
                    <button type="button" class="layui-bar layui-btn" lay-filter='SaveBtn' id="SaveBtn">保存</button>
                </div>

            </div>
           
        </form>

        
    </div>

</body>
</html>
